简介
Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。
Spring Security当前支持与所有以下技术的身份验证集
- HTTP BASIC authentication headers (an IETF RFC-based standard)
- HTTP Digest authentication headers (an IETF RFC-based standard)
- HTTP X.509 client certificate exchange (an IETF RFC-based standard)
- LDAP (a very common approach to cross-platform authentication needs, especially in large environments)
- Form-based authentication (for simple user interface needs)
- OpenID authentication
- Authentication based on pre-established request headers (such as Computer Associates Siteminder)
- JA-SIG Central Authentication Service (otherwise known as CAS, which is a popular open source single sign-on system)
- Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (a Spring remoting protocol)
- Automatic “remember-me” authentication (so you can tick a box to avoid re-authentication for a predetermined period of time)
- Anonymous authentication (allowing every unauthenticated call to automatically assume a particular security identity)
- Run-as authentication (which is useful if one call should proceed with a different security identity)
- Java Authentication and Authorization Service (JAAS)
- JEE container autentication (so you can still use Container - Managed Authentication if desired)
- Kerberos
- Java Open Source Single Sign On (JOSSO) *
- OpenNMS Network Management Platform *
- AppFuse *
- AndroMDA *
- Mule ESB *
- Direct Web Request (DWR) *
- Grails *
- Tapestry *
- JTrac *
- Jasypt *
- Roller *
- Elastic Path *
- Atlassian Crowd *
- Your own authentication systems (see below)
涉及以下系统组件:
客户端:它可以是任何平台上的任何Web服务使用者。简而言之,它可以是另一个WebService,UI应用程序或Mobile平台,它们希望通过应用程序以安全的方式读写数据。
授权服务器:验证用户凭据,并颁发令牌。它根据不同的授予类型发行令牌。下面列出了最常见的OAuth 2.0授权类型:
- Authorization Code
- Implicit
- Password
- Client Credentials
- Device Code
- Refresh Token
名词解释可以看SpringOauth2官方文档
架构设计
名词解释
– WebSecurityConfigurerAdapter 是我们安全实施的关键。它提供HttpSecurity配置以配置cors,csrf,会话管理以及受保护资源的规则。我们还可以扩展和定制包含以下元素的默认配置。
– UserDetailsService 接口有一种方法可以按用户名加载User并返回UserDetailsSpring Security可以用于认证和验证的对象。
– UserDetails 包含用于构建身份验证对象的必要信息(例如:用户名,密码,授权机构)。
– UsernamePasswordAuthenticationToken 从登录请求中获取{用户名,密码},AuthenticationManager将使用它来认证登录帐户。
– AuthenticationManager 有一个DaoAuthenticationProvider(与帮助UserDetailsService&PasswordEncoder)来验证UsernamePasswordAuthenticationToken对象。如果成功,则AuthenticationManager返回完全填充的身份验证对象(包括授予的权限)。
– OncePerRequestFilter 对我们API的每个请求执行一次。它提供了一种doFilterInternal()方法,我们将实现解析和验证JWT,加载用户详细信息(使用UserDetailsService),检查授权(使用UsernamePasswordAuthenticationToken)。
– AuthenticationEntryPoint 当客户端未经身份验证访问受保护的资源时,将捕获未经授权的错误并返回401。
认证流程
实践
引入maven包
1 | "1.0" encoding="UTF-8" xml version= |
核心代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26package org.springframework.security.oauth.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author dinghuang123@gmail.com
* @since 2020/10/20
*/
public class CustomerAuthenticationEntryPointConfiguration implements AuthenticationEntryPoint {
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
//处理异常消息通知
httpServletResponse.getWriter().write("暂无权限访问");
}
}
1 | package org.springframework.security.oauth.config; |
1 | package org.springframework.security.oauth.config; |
1 | package org.springframework.security.oauth.config; |
1 | package org.springframework.security.oauth.config; |
1 | package org.springframework.security.oauth.config; |
1 | package org.springframework.security.oauth.config.token; |
1 | package org.springframework.security.oauth.config.token; |
1 | package org.springframework.security.oauth.config.token; |
1 | package org.springframework.security.oauth.filter; |
1 | package org.springframework.security.oauth.properties; |
1 | package org.springframework.security.oauth.properties; |
1 | server: |
验证
1 | curl -H "Content-Type: application/json" -X POST "http://localhost:8090/oauth/token?client_id=client&client_secret=client&grant_type=pwssword&username=admin&password&admin" |